package com.fei.yuan.myapplication;

import com.fei.yuan.myapplication.database.FormBean;
import com.fei.yuan.myapplication.database.FormDatabase;
import com.fei.yuan.myapplication.utils.ComponentProviderUtils;
import com.fei.yuan.myapplication.utils.DatabaseUtils;
import com.fei.yuan.myapplication.utils.DateUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentProvider;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.event.notification.NotificationRequest;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @since 2021-07-22
 */
public class TimerAbility extends Ability {

    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x0, "TAG---Service");

    private static final long PERIOD = 1000L;
    private static final int NOTIFICATION_ID = 1001;
    private DatabaseHelper databaseHelper = new DatabaseHelper(this);
    private OrmContext ormContext;

    @Override
    protected void onStart(Intent intent) {
        HiLog.info(TAG, "onStart");

        ormContext = databaseHelper.getOrmContext("FormBean", "FormDatabase.db", FormDatabase.class);
        startTimer();
        super.onStart(intent);
    }

    private void startTimer(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                upDateFormBean();
            }
        },0,PERIOD);
    }

    private void upDateFormBean() {
        OrmPredicates ormPredicates = new OrmPredicates(FormBean.class);
        List<FormBean> beanList = ormContext.query(ormPredicates);
        if (beanList.size() <= 0) {
            return;
        }
        for (FormBean formBean : beanList) {
            //更新卡片信息
            ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(formBean,this);
            try {
                updateForm(formBean.getFormId(),componentProvider);
            } catch (FormException e) {
                DatabaseUtils.deleteFormData(formBean.getFormId(),ormContext);
            }
        }
    }


    private void notice(){
        NotificationRequest request = new NotificationRequest(NOTIFICATION_ID);
        request.setAlertOneTime(true);
        NotificationRequest.NotificationNormalContent normalContent = new NotificationRequest.NotificationNormalContent();
        normalContent.setText(DateUtils.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
        NotificationRequest.NotificationContent content = new NotificationRequest.NotificationContent(normalContent);
        request.setContent(content);
        keepBackgroundRunning(NOTIFICATION_ID,request);
    }


    @Override
    protected void onBackground() {
        HiLog.info(TAG, "onBackground");
        super.onBackground();
    }

    @Override
    protected void onStop() {
        HiLog.info(TAG, "onStop");
        super.onStop();
    }
}
